extends CanvasLayer

var score = 0
var time = 100
var lives = 3

@onready var scoreLabel: Label  = $Score
@onready var liveLabel: Label  = $Live
@onready var playerCamera: Camera2D  = get_parent()
@onready var viewport_size = get_viewport().size

func _ready():
	EventBus.connect("coin_collected", _on_coin_collected)  # 新增事件监听
	EventBus.connect("player_heath", _lives_init)  # 新增事件监听
	label_scale_handle(scoreLabel)
	label_scale_handle(liveLabel)

func _lives_init(new_lives: int = 3):
	lives = new_lives

func _on_coin_collected():
	score += 1
	# 如果需要在UI显示金币数量，可以添加coinsLabel并在此更新

func _process(delta):
	scoreLabel.text = "Score: " + str(score)
	liveLabel.text = "Live: " + str(lives)
	if playerCamera: 
		offset.x = playerCamera.position.x - viewport_size.x / (2 * playerCamera.zoom.x)
		offset.y = playerCamera.position.y - viewport_size.y / (2 * playerCamera.zoom.y)

func label_scale_handle(label):
	label.scale.x /= playerCamera.zoom.x * 1.0
	label.scale.y /= playerCamera.zoom.y * 1.0
